package imp;

import dao.SearchRecordDao;
import model.Goods;
import model.Trading;
import util.DUBtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SearchRecord_imp implements SearchRecordDao {
    @Override
    public boolean select(String content) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Trading trading = new Trading();
        String sql = "select * from searchcontent where content=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, content);
            System.out.println("搜索记录查找SQL语句:"+pstmt);
            rs = pstmt.executeQuery();
            if (rs.next()){
                return false;
            }
            rs.close();
            pstmt.close();
            con.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean add(String id, String content) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "insert into searchcontent(id,content,count) values(?,?,1)";
        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.setString(2, content);
            System.out.println("添加新搜索词修改SQL语句:"+pstmt);
            pstmt.executeUpdate();
            pstmt.close();
            connection.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean addcount(String content) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "update searchcontent set count=count+1 where content=?";
        try {
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, content);
            System.out.println("搜索词次数修改SQL语句:"+pstmt);
            pstmt.executeUpdate();
            pstmt.close();
            connection.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<String> returncontent() {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Trading trading = new Trading();
        String sql = "select content from searchcontent order by count desc limit 20 ";
        List<String> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                list.add(rs.getString("content"));
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }
}
